Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Raising a number to a power (the Y^X button) is an equally simple calculation, requiring only Visual Basic’s ^ operator. Note that the number on the stack is raised to the power of the number in the display—not the other way around. The code for this event procedure is shown in Listing 5.13. Listing 5.13 The Y^X button Click event procedure.
    Private Sub cmdPower_Click()

    ‘ Raises the number on the stack to the power
    ‘ of the number in the display.

    Dim Result As Double

    If StackPointer < 1 Then Exit Sub

    Result = Stack(StackPointer) ^ txtDisplay.Text
    DisplayResult (Result)
    NewEntry = True
    DisplayHasData = True

    End Sub
Finally, we come to the Copy button’s Click event procedure. Now, how the devil do you copy something to the Windows Clipboard? You may think it’s complicated, but it’s actually very simple. In a Visual Basic program, you have direct access to the Windows Clipboard object, which is named (guess) Clipboard. Several methods are available for working with the Clipboard object. One method is SetText, which places the specified text on the Clipboard, where any Windows program can retrieve it with a Paste command. You can see from Listing 5.14 how simple the code is, requiring only a single line. We’ll be using some of the other Clipboard methods in other parts of the book. Take a look at the Visual Basic Help system for more information. Listing 5.14 The Copy button Click event procedure.
    Private Sub cmdCopy_Click()

    ‘ Put the display text on the Windows clipboard

    Clipboard.SetText txtDisplay.Text

    End Sub
Believe it or not, the calculator is finished. Take it for a spin. Figure 5.8 shows the calculator in action. Creating the calculator has been a fairly involved project, but only because you are new to Visual Basic. In comparison with what is possible with Visual Basic, the calculator is really quite simple. By this point, you already have many of the most important Visual Basic fundamentals under your belt. I encourage you to start experimenting with Visual Basic, designing and creating small projects on your own. If you want to become proficient at programming, there’s no substitute for practice. Figure 5.8 The completed calculator executing. ### Creating An Executable As you develop a Visual Basic program, you run it within the Visual Basic development environment. This gives you access to several development tools the environment provides, such as the Immediate window and debugging tools (debugging tools are covered in Chapter 26). What happens, though, when the program is complete? You can’t just assume that all of your customers will have Visual Basic; will they be able to run the program without it? Yes, indeed. Once you have put the finishing touches on a program, the next step is to create an executable file. A program in this form can be executed independently from the Visual Basic development environment. You can distribute the executable program without restriction. To create an executable file from your Visual Basic project, select Make Progname.exe from the File menu (Progname being the name that you have assigned to the project). Visual Basic will display the Make Project dialog box, shown in Figure 5.9. The large box will display the names of any other executable files in the current folder, and the assigned project name will be automatically entered in the File Name box. You can change to a different folder, if desired. You can also change the name of the executable file. Select OK, and Visual Basic will start creating the executable. The amount of time this takes will depend on the size and complexity of your project and the speed of your computer. When the process is complete, the executable file will be in the specified folder. You can use the usual Windows 95 techniques to create a shortcut to the program. Figure 5.9 The Make Project dialog box. TIP: Choosing A Project Icon Visual Basic will assign a default icon to your project, but you can do better. As you probably know, this icon is displayed for the program’s shortcuts and also when it is minimized. Select the project’s main form and use its Icon property to select an icon (kept on disk in files with the .ICO extension). Visual Basic comes with an extensive icon library. Where these files are located depends on the version of Visual Basic you installed. Look for a Graphics\Icons folder. While an executable Visual Basic program is independent of the Visual Basic development environment, it is not totally standalone. All Visual Basic executable programs depend on one or more other files to execute. Because you have Visual Basic installed on your system, these files are present, and you can run any Visual Basic executable without a problem. If you give a Visual Basic executable to a friend, he or she may not have these files on his or her system and will not be able to run your program. Determining which files need to be distributed with your programs is not an easy matter. The easiest way to distribute Visual Basic applications is through the Visual Basic Application Setup Wizard, a topic covered in Chapter 26. #### Compilation Options The process of creating an executable file is called compilation. During compilation, Visual Basic translates the English-like statements of your source code and the visual specifications of your interface design into the binary code that can be understood by the computer’s CPU. When you compile a Visual Basic project, several options are available to you. Some of the compilation options are advanced and rarely, if ever, need to be changed. A few are worth noting, however. To access the options, select Make XXX.exe to display the Make Project dialog box, as you learned above. Click on the Options button to display the Project Properties dialog box, and if necessary, click on the Compile tab, as shown in Figure 5.10. The two main options are as follows: • Compile to P-code—The P stands for pseudo, meaning that the compilation process will perform a partial translation of the project source code into pseudocode. When the program executes, the remainder of the translation process is performed. Until version 5 of Visual Basic, the only type of compilation available was P-code. • Compile to Native Code—The compilation process will perform a complete translation of your source code. No further translation is required when the program executes. What’s the difference between these two compilation modes? Because of the extra translation step required, P-code runs more slowly than native code. In fact, this was a major complaint with earlier versions of Visual Basic: Its programs were always a bit slower than equivalent programs written in C++, Delphi, or other languages that use native code. P-code programs tend to have smaller executable file sizes, but with today’s gargantuan hard disks, this is rarely a concern. From my perspective, there’s no reason for ever choosing P-code over native code.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.